package wisp.game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import wisp.dataObjects.*;
//GTGE
import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.*;
import com.golden.gamedev.object.background.ImageBackground;
import com.golden.gamedev.engine.timer.*;

public class MyGameLoader extends MouseAdapter{


	/**** LOG IN SCREEN ******/
	JFrame logInScreen;
	
	JTextField usernameTxt;
	JTextField passwordTxt;
	
	JLabel usernameLbl;
	JLabel passwordLbl;
	
	JButton enterButton;
	JButton exitButton;
	JButton createNewButton;
	
	MyPanel panel;

	ImageIcon banner;
	/**** END LOG IN SELECT ******/
	
	/**** CHARACTER SELECT ******/
	JFrame characterNameScreen;
	JFrame characterClassScreen;
	JFrame characterGenderScreen;
	JFrame characterLineageScreen;
	
	ButtonGroup classSelectGroup;
	ButtonGroup genderSelectGroup;
	ButtonGroup lineageSelectGroup;
	
	JTextField characterNameTxt;
	JLabel characterNameLbl;
	
	JRadioButton templarRButton;
	JRadioButton elementalistRButton;
	JRadioButton heirophantRButton;
	JRadioButton femaleRButton;
	JRadioButton maleRButton;
	JRadioButton fasciaRButton;
	JRadioButton bellumRButton;
	
	//class screen pictures
	JLabel templarIcon;
	JLabel elementalistIcon;
	JLabel heirophantIcon;
	JLabel templarDescription;
	JLabel elementalistDescription;
	JLabel heirophantDescription;
	
	//gender screen pictures
	JLabel femaleTemplar;
	JLabel maleTemplar;
	JLabel femaleElementalist;
	JLabel maleElementalist;
	JLabel femaleHeirophant;
	JLabel maleHeirophant;
	
	//lineage screen pictures
	JLabel fasciaIcon;
	JLabel bellumIcon;
	
	//character name screens
	JLabel classLbl;
	JLabel genderLbl;
	JLabel lineageLbl;
	JLabel characterIcon;
	
	JLabel lineageBanner;
	
	JButton createButton;
	JButton checkButton;
	JButton cancelClassButton;
	JButton cancelGenderButton;
	JButton backGenderButton;
	JButton cancelLineageButton;
	JButton backLineageButton;
	JButton cancelNameButton;
	JButton backNameButton;
	JButton nextClassButton;
	JButton nextGenderButton;
	JButton nextLineageButton;
	
	JPanel panelClass;
	JPanel panelGender;
	JPanel panelLineage;
	JPanel panelCharName;
	/**** END CHARACTER SELECT ******/

	/**** VALIDATING OBJECTS *****/
	WispUser wispUser;
	WispCharacter wispCharacter;
	String strClass;
	String gender;
	String lineage;
	String characterName;
	
	/***** important temp variables ****/
	String username;

	private boolean gameStart;

	private MainGame game;
	
	public MyGameLoader(MainGame game)
	{
		wispUser = new WispUser();
		wispCharacter = new WispCharacter();
			initLogInScreen();
			initClassSelectScreen();
			initGenderSelectScreen();
			initLineageSelectScreen();
			initNameSelectScreen();
			
			strClass = "none";
			this.game = game;
	}
	
	public void initLogInScreen()
	{
		banner = new ImageIcon("something.png");
		
		logInScreen = new JFrame("Wisp Online");
		logInScreen.setSize (500, 500);
		
		panel = new MyPanel();
		panel.setLayout(null);
		
		usernameTxt = new JTextField("");
		usernameTxt.setBounds (190, 200, 200, 25);
		panel.add(usernameTxt);
			
		passwordTxt = new JTextField("");
		passwordTxt.setBounds (190, 230, 200, 25);
		panel.add(passwordTxt);	
			
		usernameLbl = new JLabel ("User Name:");
		usernameLbl.setBounds (90, 200, 100, 25);
		panel.add(usernameLbl);	
				
		passwordLbl = new JLabel ("Password: ");
		passwordLbl.setBounds (90, 230, 100, 25);
		panel.add(passwordLbl);	
		
		createNewButton = new JButton ("Create New Button");
		createNewButton.setBounds (135, 265, 85, 30);
		createNewButton.addMouseListener(this);
		panel.add(createNewButton);
			
		enterButton = new JButton ("Enter");
		enterButton.setBounds (215, 265, 85, 30);
		enterButton.addMouseListener(this);
		panel.add(enterButton);
		
		exitButton = new JButton ("Exit");
		exitButton.setBounds(300, 265, 85, 30);
		panel.add(exitButton);
		
		logInScreen.add(panel);
		logInScreen.setVisible(true);
	}
	
	public void initClassSelectScreen()
	{	
		characterClassScreen = new JFrame("Wisp Online");
		characterClassScreen.setSize (630, 700);
		characterClassScreen.setLayout(null);
		
		classSelectGroup = new ButtonGroup();
		
		panelClass = new JPanel();
		panelClass.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Character Class")));
		panelClass.setBounds(50,50, 500, 500);
		panelClass.setLayout(null);
		
		templarRButton = new JRadioButton();
		templarRButton.setBounds (50, 50, 30, 30);
		panelClass.add(templarRButton);
		
		elementalistRButton = new JRadioButton();
		elementalistRButton.setBounds (50, 200, 30, 30);
		panelClass.add(elementalistRButton);
		
		heirophantRButton = new JRadioButton();
		heirophantRButton.setBounds (50, 350, 30, 30);
		panelClass.add(heirophantRButton);

		templarIcon = new JLabel ("Templar");
		templarIcon.setIcon(new ImageIcon ("IMAGES/tIcon.png"));
		templarIcon.setBounds(100, 30,300, 100);
		panelClass.add(templarIcon);
		
		elementalistIcon  = new JLabel ("Elementalist");
		elementalistIcon.setIcon(new ImageIcon ("IMAGEs/eIcon.png"));
		elementalistIcon.setBounds(100, 170, 300, 100);
		panelClass.add(elementalistIcon);
		
		heirophantIcon  = new JLabel ("Heirophant");
		heirophantIcon.setIcon(new ImageIcon ("IMAGES/hIcon.png"));
		heirophantIcon.setBounds(100, 320, 300, 100);
		panelClass.add(heirophantIcon);
		
		nextClassButton = new JButton("Next");
		nextClassButton.setBounds (300, 460, 85, 30);
		nextClassButton.addMouseListener(this);
		panelClass.add(nextClassButton);
		
		cancelClassButton = new JButton("Cancel");
		cancelClassButton.setBounds (390, 460, 85, 30);
		cancelClassButton.addMouseListener(this);
		panelClass.add(cancelClassButton);
		
		classSelectGroup.add (templarRButton);
		classSelectGroup.add (elementalistRButton);
		classSelectGroup.add (heirophantRButton);
		
		characterClassScreen.add(panelClass);
	}

	public void initNameSelectScreen()
	{
		characterNameScreen = new JFrame("Wisp Online");
		characterNameScreen.setSize (450, 450);
		characterNameScreen.setLayout(null);
		
		panelCharName = new JPanel();
		panelCharName.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Character Name")));
		panelCharName.setBounds(10, 10,400, 400);
		panelCharName.setLayout(null);
	
		classLbl = new JLabel();
		
		panelCharName.add(classLbl);
		
		genderLbl = new JLabel("Gender: " + this.gender);
		
		panelCharName.add(genderLbl);
		
		lineageLbl = new JLabel("Lineage: " + this.lineage);
		
		panelCharName.add(lineageLbl);

		/*** CHARACTER NAME PANEL *****/
		characterNameTxt = new JTextField();	
		characterNameLbl = new JLabel("Enter Character Name: ");		
		checkButton = new JButton("Check If Existing");
		createButton = new JButton("Create");
		
		/*** END CHARACTER NAME PANEL *****/
		
		characterNameScreen.add(panelCharName);
		
	}
	
	public void initGenderSelectScreen()
	{
		characterGenderScreen = new JFrame("Wisp Online");
		characterGenderScreen.setSize (550, 550);
		characterGenderScreen.setLayout(null);
		
		panelGender = new JPanel();
		panelGender.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Character Gender")));
		panelGender.setBounds(50, 50, 400, 400);
		panelGender.setLayout(null);
		
		genderSelectGroup = new ButtonGroup();
		
		/***** CHARACTER GENDER PANEL ************/
		femaleRButton = new JRadioButton("Female");

		//kulang ng JLabels for the icons o.o kasi picture ng female and male version for each class
		maleRButton = new JRadioButton("Male");
		
		/***** END CHARACTER GENDER PANEL************/
		
		 femaleTemplar = new JLabel();
		 femaleTemplar.setIcon(new ImageIcon("IMAGES/femaletemplar.png"));
		 maleTemplar = new JLabel();
		 maleTemplar.setIcon(new ImageIcon("IMAGES/maletemplar.png"));
		 femaleElementalist = new JLabel();
		 femaleElementalist.setIcon(new ImageIcon("IMAGES/femaleElementalist.png"));
		 maleElementalist = new JLabel();
		 maleElementalist.setIcon(new ImageIcon("IMAGES/maleelementalist.png"));
		 femaleHeirophant = new JLabel();
		 femaleHeirophant.setIcon(new ImageIcon ("IMAGES/femaleheirophant.png"));
		 maleHeirophant = new JLabel();
		 maleHeirophant.setIcon(new ImageIcon ("IMAGES/maleheirophant.png"));
		 
		 backGenderButton = new JButton("Back");
		 backGenderButton.addMouseListener(this);
	
		 cancelGenderButton = new JButton("Cancel");
		 cancelGenderButton.addMouseListener(this);
		 
		 nextGenderButton = new JButton("Next");
		 nextGenderButton.addMouseListener(this);
		 
		 genderSelectGroup.add (femaleRButton);
		 genderSelectGroup.add (maleRButton);
		 characterGenderScreen.add(panelGender);
		
	}
	
	public void initLineageSelectScreen()
	{

		characterLineageScreen = new JFrame("Wisp Online");
		characterLineageScreen.setSize (630, 700);
		characterLineageScreen.setLayout(null);
		
		panelLineage = new JPanel();
		panelLineage.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Character Lineage")));
		panelLineage.setBounds(50, 50, 500, 500);
		
		panelLineage.setLayout(null);
		
		lineageSelectGroup = new ButtonGroup();
		
		/******* CHARACTER LINEAGE PANEL ******/
		fasciaRButton = new JRadioButton("Fascia");
		fasciaRButton.setBounds (50, 15, 150,30);
		panelLineage.add(fasciaRButton);
		
		//kulang ng icons/logo for each lineage that would be used using JLabel
		
		bellumRButton = new JRadioButton("Bellum");
		bellumRButton.setBounds (50, 200, 150, 30);
		panelLineage.add(bellumRButton);
		
		/******* CHARACTER LINEAGE PANEL ******/
		
		fasciaIcon = new JLabel();
		fasciaIcon.setIcon(new ImageIcon ("IMAGES/fascia.PNG"));
		fasciaIcon.setBounds (50,50, 400, 100);
		panelLineage.add (fasciaIcon);
		
		bellumIcon = new JLabel();
		bellumIcon.setIcon(new ImageIcon ("IMAGES/bellum.PNG"));
		bellumIcon.setBounds (50, 230, 400, 100);
		panelLineage.add(bellumIcon);
		
		nextLineageButton = new JButton("Next");
		nextLineageButton.setBounds (300, 420, 85, 30);
		nextLineageButton.addMouseListener(this);
		panelLineage.add(nextLineageButton);
		
		backLineageButton = new JButton("Back");
		backLineageButton.setBounds (210, 420, 85, 30);
		backLineageButton.addMouseListener(this);
		panelLineage.add(backLineageButton);
		
		cancelLineageButton = new JButton("Cancel");
		cancelLineageButton.setBounds (390, 420, 85, 30);
		cancelLineageButton.addMouseListener(this);
		panelLineage.add(cancelLineageButton);
		
		lineageSelectGroup.add (fasciaRButton);
		lineageSelectGroup.add (bellumRButton);
		characterLineageScreen.add(panelLineage);
	
	}
	
	public class MyPanel extends JPanel
	{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public void paint(Graphics g)
		{
		
			g.drawImage(banner.getImage(), 0, 0 , 500, 500, this);
			this.paintChildren (g);
			
		}
	}
	
	/*******EVENTS********/
	
	public void mouseClicked(MouseEvent e)
	{
		if (e.getSource() == enterButton)
		{
			this.username = usernameTxt.getText();
			String password = passwordTxt.getText();
			
			/** validate fields **/
			if (this.username.equals("") == true || password.equals("") == true)
				JOptionPane.showMessageDialog(characterNameScreen, "You cannot leave that field empty", "Message", 0);
			
			//check if username and password is valid
			else if (wispUser.validateUser(this.username,  password))
			{
				
				//no character created -> go to character select
				if (this.wispCharacter.viewCharUsingUname(this.username) == false)
				{
					logInScreen.setVisible(false);
					characterClassScreen.setVisible(true);
				}

				else
				{ 
					System.out.println("lalala");
					this.gameStart = true;
				}
					
				
			}
		
		}// end log in screen load
		
		else if (e.getSource() == createNewButton)
		{
			characterClassScreen.setVisible(true);
			logInScreen.setVisible(false);
		}
		
		else if (e.getSource() == nextClassButton)
		{
			if (templarRButton.isSelected() == false && elementalistRButton.isSelected() == false && heirophantRButton.isSelected() == false)
				JOptionPane.showMessageDialog(characterNameScreen, "Please your character's class.", "Message",0);
			else
			{
				if (templarRButton.isSelected() == true)
					this.strClass = "templar";
				else if (elementalistRButton.isSelected() == true)
					this.strClass = "elementalist";
				else if (heirophantRButton.isSelected() == true)
					this.strClass = "heirophant";
				
				characterClassScreen.setVisible(false);
				updateGenderScreen();
				characterGenderScreen.setVisible(true);
			}
			//	charClassCheck = true;
		}
		
		else if (e.getSource() == cancelClassButton)
		{
			characterClassScreen.setVisible(false);
			logInScreen.setVisible(true);
		}
		
		else if (e.getSource() == nextGenderButton)
		{
			if (femaleRButton.isSelected() == false && maleRButton.isSelected() == false )
				JOptionPane.showMessageDialog(characterNameScreen, "Please select your character's gender.", "Message",0);
			else
			{
				if (femaleRButton.isSelected() == false)
					gender = "Female";
				else
					gender = "Male";
				
				characterGenderScreen.setVisible(false);
				characterLineageScreen.setVisible(true);
			}		
			
			//charGenderCheck = true;
		}
		
		else if (e.getSource() == cancelGenderButton)
		{
			characterGenderScreen.setVisible(false);
			logInScreen.setVisible(true);
		}
		
		else if (e.getSource() == backGenderButton)
		{
			characterGenderScreen.setVisible(false);
			characterClassScreen.setVisible(true);
		}
		
		else if (e.getSource() == nextLineageButton)
		{
			if (fasciaRButton.isSelected() == false && bellumRButton.isSelected() == false )
				JOptionPane.showMessageDialog(characterNameScreen, "Please select your character's lineage.", "Message",0);
			else
			{
				if (fasciaRButton.isSelected() == true)
					lineage = "Fascia";
				else
					lineage = "Bellum";
				
				characterLineageScreen.setVisible(false);
				updateNameScreen();
				characterNameScreen.setVisible(true);
			}
		}
		
		else if (e.getSource() == cancelLineageButton)
		{
			characterLineageScreen.setVisible(false);
			logInScreen.setVisible(true);
		}
		
		else if (e.getSource() == backLineageButton)
		{
			characterLineageScreen.setVisible(false);
			characterGenderScreen.setVisible(true);
		}
		else if (e.getSource() == createButton)
		{			
			 this.characterName = characterNameTxt.getText();
			
			/** validate fields **/
			if (characterName.equals(""))
				JOptionPane.showMessageDialog(characterNameScreen, "Please enter a character name", "Message",0);
				
			else if(wispCharacter.checkCnameExist(characterName))
				JOptionPane.showMessageDialog(characterNameScreen, "A character with that name already exists.", "Message",0);
			/** end validate fields **/	
			
			else
			{
				//add to database the newly acquired information
				this.wispCharacter = createNewWispCharacter (this.characterName, this.gender, this.lineage, this.strClass);
				createNewStatistics (characterName, strClass);		
				
				characterNameScreen.setVisible(false);
				this.gameStart = true;
			}				
		}
		
		else if (e.getSource() == cancelNameButton)
		{
			characterNameScreen.setVisible(false);
			logInScreen.setVisible(true);
		}

	}
	
	//adds to the database the copy of wispcharacter
	public WispCharacter createNewWispCharacter(String charactername, String gender, String lineage, String strClass)
	{
		int initHP, initMP;
		double favorPts;
		initHP = initMP = 0;
		WispCharacter wispCharacter;
		
		if (lineage.equals("Fascia") == true)
			favorPts = 3;
		else
			favorPts = -3;
			
		if (strClass.equals("templar"))
		{
			initHP = 23;
			initMP = 11;
		}
		
		else if (strClass.equals("elementalist"))
		{
			initHP = 12;
			initMP = 22;
		}
		
		else if (strClass.equals("heirophant"))
		{
			initHP = 15;
			initMP = 19;
		}
			
		wispCharacter  = new WispCharacter (this.username, 
											charactername,
											favorPts,
											0,					// x Coordinates
											0,					// y Coordinates
											strClass,
											"Alive",
											"No",				//if in a party
											"None",				//guild name
											"yes",				//if online in virtual world
											"Field1",			//current map 
											gender,				
											1,					// level
											0,					//experience points
											100,
											initHP,
											initHP,
											initMP,
											initMP,
											0,					//current weight
											50,					//max weight
											0,					//rank
											3);					//heal points
											
		wispCharacter.addCharacter(wispCharacter);
		
		return wispCharacter;
	}
	
	public void createNewStatistics (String charactername, String strClass)
	{
		int str ;
		int	dex ;
		int	nInt ;
		int	vit ;
		int spr ;
		
		str = dex = nInt = vit = spr = 0;
		if (strClass.equals("templar"))
		{
			str = 5;
			dex = 2;
			nInt = 1;
			vit = 3;
			spr = 2;
		}
		
		else if (strClass.equals("elementalist"))
		{
			str = 1;
			dex = 4;
			nInt = 5;
			vit = 2;
			spr = 3;
		}
		
		else if (strClass.equals("heirophant"))
		{
			str = 2;
			dex = 1;
			nInt = 2;
			vit = 3;
			spr = 5;
		}
		
		Statistics statistics = new Statistics(charactername,
											   str,
											   dex,
											   nInt,
											   vit,
											   spr,
											   1,
											   1,
											   1,
											   1,
											   1.5,
											   1.0,
											   1.0,
											   1.0,
											   1.0,
											   55.0,
											   0.5,
											   1);
		
		statistics.addStatistics(statistics);
		
	}
	
	public boolean startGame()
	{
		//GameLoader game = new GameLoader();

     //  game.setup(new MainGame(wispCharacter.getCharactername(), wispCharacter.getCClass()), new Dimension(800,600), false);

     //  game.start();
	game.setCharacterClass(wispCharacter.getCClass());
	game.setCharacterName(wispCharacter.getCharactername());
			return gameStart;
		
	}

	public void updateGenderScreen()
	{
		panelGender.removeAll();
		
		if (this.strClass.equals("templar") == true)
		{
			femaleTemplar.setBounds (50, 50, 300, 150);
			panelGender.add(femaleTemplar);
			maleTemplar.setBounds (50, 195, 300, 150);
			panelGender.add(maleTemplar);
		}
		
		else if (this.strClass.equals("elementalist") == true)
		{
			femaleElementalist.setBounds (50, 50, 300, 150);
			panelGender.add(femaleElementalist);
			maleElementalist.setBounds (50, 195, 300, 150);
			panelGender.add(maleElementalist);
		}
			
		else if (this.strClass.equals("heirophant") == true)
		{
			femaleHeirophant.setBounds (50, 50, 300, 150);
			panelGender.add(femaleHeirophant);
			maleHeirophant.setBounds (50, 195, 300, 150);
			panelGender.add(maleHeirophant);
		}
		cancelGenderButton.setBounds (300, 360, 75, 30);
		panelGender.add(cancelGenderButton);
		
		nextGenderButton.setBounds (215, 360, 75, 30);
		panelGender.add(nextGenderButton);
		 
		backGenderButton.setBounds (130, 360, 75, 30);
		panelGender.add(backGenderButton);

		maleRButton.setBounds (50, 190, 100, 30);
		panelGender.add(maleRButton);
		femaleRButton.setBounds (50, 50, 100, 30);
		panelGender.add(femaleRButton);
		
	}
	
	public void updateNameScreen()
	{
		panelCharName.removeAll();
		if (this.strClass.equals("templar") == true)
		{
			if (this.gender.equals("Female") == true)
			{
				femaleTemplar.setBounds (50, 150, 300, 150);
				panelCharName.add(femaleTemplar);
			}
			else
			{
				maleTemplar.setBounds (50, 150, 300, 150);
				panelCharName.add(maleTemplar);
			}
		}
		
		else if (this.strClass.equals("elementalist") == true)
		{
			if (this.gender.equals("Female") == true)
			{
				femaleElementalist.setBounds (50, 150, 300, 150);
				panelCharName.add(femaleElementalist);
			}
			else
			{
				maleElementalist.setBounds (50, 150, 300, 150);
				panelCharName.add(maleElementalist);
			}
		}
			
		else if (this.strClass.equals("heirophant") == true)
		{
			if (this.gender.equals("Female") == true)
			{
				femaleHeirophant.setBounds (50, 150, 300, 150);
				panelCharName.add(femaleHeirophant);
			}
			else
			{
				maleHeirophant.setBounds (50, 150, 300, 150);
				panelCharName.add(maleHeirophant);
			}
		}
		
		classLbl.setText("Class: " + this.strClass);
		classLbl.setBounds (10, 50, 150, 30);
		panelCharName.add(classLbl);
		
		genderLbl.setText("Gender:  " + this.gender);
		genderLbl.setBounds (10, 90, 100, 30);
		panelCharName.add(genderLbl);
		
		lineageLbl.setText("Lineage:  " + this.lineage);
		lineageLbl.setBounds (10, 130, 100, 30);
		panelCharName.add(lineageLbl);
		
		characterNameTxt.setBounds (150, 20, 200, 25);
		panelCharName.add(characterNameTxt);
		
		characterNameLbl.setBounds (10, 17, 150, 30);
		panelCharName.add(characterNameLbl);
		
		checkButton.setBounds (200, 350, 150, 30);
		panelCharName.add(checkButton);
		checkButton.addMouseListener(this);
		
		createButton.setBounds (50, 350, 150, 30);
		panelCharName.add(createButton);
		createButton.addMouseListener(this);
	}
	
	
}